package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository destinationRepository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IRegionService regionService;


    @Override
    public void save(Destination destination) {
        destination.setId(null);
        destinationRepository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return destinationRepository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    @Override
    public List<Destination> queryDestByRegionId(String rid) {
        //根据区域id查询区域对象
        Region region = regionService.get(rid);
        //获取对象挂载的目的地的id集合
        List<String> ids = region.getRefIds();
        //通过ids 集合查询目的地集合  JPA方式
        return destinationRepository.findByIdIn(ids);
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {

        Query query = new Query();
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }

        else if (StringUtils.hasLength(qo.getParentId())) {
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        } else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }
        long totalCount = template.count(query, Destination.class);

        if (totalCount == 0) {
            return Page.empty();
        }

        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.ASC, "_id");
        query.with(pageable);
        List<Destination> list = template.find(query, Destination.class);
        return new PageImpl<>(list, pageable, totalCount);
    }

    @Override
    public List<Destination> queryToasts(String parentId) {
        List<Destination> list = new ArrayList<>();
        //创建一个方法，用于创建toasts 对象
        this.createToasts(list, parentId);

        Collections.reverse(list);
        return list;
    }
    private void createToasts(List<Destination> list, String parentId) {
        //parentId 为空时，直接返回
        if (!StringUtils.hasLength(parentId)) {
            return;
        }
        Destination dest = this.get(parentId);
        list.add(dest);
        if (StringUtils.hasLength(parentId)) {
            createToasts(list, dest.getParentId());
        }
    }

    @Override
    public List<Destination> queryDestByRegionIdForApi(String regionId) {
        List<Destination> list = new ArrayList<>();
        //区域 国内
        if ("-1".equals(regionId)) {
            //查询中国
            list = destinationRepository.findByParentName("中国");
        } else {
            list = this.queryDestByRegionId(regionId);
        }
        for (Destination dest : list) {
            //只显示5个
            Pageable pageable=PageRequest.of(0,5,Sort.Direction.ASC,"_id");

            List<Destination> children = destinationRepository.findByParentId(dest.getId(),pageable);
            dest.setChildren(children);
        }
        return list;
    }

    @Override
    public Destination findByName(String keyword) {

        return destinationRepository.findByName(keyword);
    }
}
